import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@EnableAutoConfiguration
@Import({Hello.class})
public class MyApplication {

    @Autowired
    private String hello;

//    @Autowired
//    RestClient restClient;

    @Autowired
    RestHighLevelClient highLevelClient;

    @RequestMapping("/")
    String home() {
        return "Hello World 5555!" + hello;
    }

    public static void main(String[] args) {
        SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(MyApplication.class);
        springApplicationBuilder.lazyInitialization(true);
        //        springApplicationBuilder.web(WebApplicationType.SERVLET);
        springApplicationBuilder.web(WebApplicationType.REACTIVE);
        //        springApplicationBuilder.main(MyApplication.class);
        SpringApplication application = springApplicationBuilder
                .application();
        application
                .setApplicationStartup(new BufferingApplicationStartup(2048));
        Map<String, Object> properties = new HashMap<>();
        properties.put("biwin.test", "2222");
        application.setDefaultProperties(properties);
        application.run(args);
        //        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    public ApplicationRunner buildRunner(ApplicationArguments arg) {
        return new ApplicationRunner() {
            @Override
            public void run(ApplicationArguments args) throws Exception {
                List<String> nonOptionArgs = args.getNonOptionArgs();
                nonOptionArgs.forEach(System.out::println);
                Set<String> optionNames = args.getOptionNames();
                optionNames.forEach(name -> {
                    System.out.println("[选项参数]:" + name + "; value=" + args.getOptionValues(name));
                });
            }
        };
    }

    @Bean
    public ExitCodeGenerator buildExit() {
        return new ExitCodeGenerator() {
            @Override
            public int getExitCode() {
                return 111;
            }
        };
    }
}

